home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / Open Transport / Sample Code / DTS Sample Code / OTMultiTest / otMultiNodeTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-19  |  4.7 KB  |  182 lines  |  [TEXT/CWIE]

  1. #include     "OpenTransport.h"
  2. #include    "OpenTptLinks.h"
  3. #include    "OpenTptAppleTalk.h"
  4. #include     <Events.h>
  5. #include    <stdio.h>
  6.  
  7. #define DDP_OPT_SRCADDR    0x2101    // define this here for now.
  8. #define kDestNodeID        128
  9.  
  10. DDPAddress                reqDDPAddr;
  11. DDPAddress                retDDPAddr;
  12. TBind                    req;
  13. TBind                    ret;
  14. EndpointRef                ep = 0;
  15.  
  16. main()
  17. {
  18.     short            bOTAvaliable;
  19.     OSStatus        otErr=0;
  20.     UInt8            packet[1024];
  21.     DDPAddress        sendAddr;
  22.     DDPAddress        recvAddr;
  23.     TUnitData        otPB;
  24.     UInt8            buf[kOTOptionHeaderSize + kDDPAddressLength];    // define buffer for fourByte Option size
  25.     TOption*        opt;                        // option ptr to make items easier to access
  26.     OTFlags            flags;
  27.     long            timer;
  28.     Boolean            processIncoming;
  29.     
  30.                                 /* Initialize Open Transport.        */
  31.                                 /* NOTE - I'm not initializing any  */
  32.                                 /* other managers.  Should I?        */
  33.  
  34.     bOTAvaliable = InitOpenTransport();
  35.     if (bOTAvaliable != kOTNoError) 
  36.     {
  37.     
  38.         printf("\n failed to init OT");
  39.         return(0);
  40.     }
  41.                                 /* open the DDP endpoint            */
  42.  
  43.     ep = OTOpenEndpoint(OTCreateConfiguration("ddp"), 0, 0, &otErr);
  44.     
  45.     if (otErr || !ep) {
  46.     
  47.         printf("\n failed to open endpoint");
  48.         return(0);
  49.     }
  50.                                 /* build the DDPAddress for binding */
  51.                                 /* to Multinode AppleTalk.  The        */
  52.                                 /* network field matches our network*/
  53.                                 /* and the node ID is a value that  */
  54.                                 /* is known to be available.        */
  55.  
  56.     reqDDPAddr.fAddressType = AF_ATALK_MNODE;
  57.     reqDDPAddr.fNetwork        = 0;
  58.     reqDDPAddr.fNodeID        = 0;
  59.                                 
  60.                                 /* fill out the request and return    */
  61.                                 /* TBind blocks                        */
  62.  
  63.     req.addr.maxlen            = 0;
  64.     req.addr.len            = sizeof(DDPAddress);
  65.     req.addr.buf            = (UInt8 *)&reqDDPAddr;
  66.     req.qlen                = 0;
  67.             
  68.     ret.addr.maxlen            = sizeof(DDPAddress);
  69.     ret.addr.len            = 0;
  70.     ret.addr.buf            = (UInt8 *)&retDDPAddr;
  71.     ret.qlen                = 0;
  72.             
  73.                                 /* bind the endpoint                 */
  74.  
  75.     otErr = OTBind(ep, &req, &ret);
  76.     if (otErr != kOTNoError) 
  77.     {
  78.     
  79.         printf("\n Failed to bind ");
  80.         OTCloseProvider(ep);
  81.         return(0);
  82.     }
  83.     else
  84.     {
  85.         printf("\n\nBind successful, node %d acquired.", retDDPAddr.fNodeID);
  86.     }
  87.                                 /* build a AppleTalk Echo Protocol    */
  88.                                 /* packet to be sent to the local   */
  89.                                 /* network node 0x42.  While 0x42   */
  90.                                 /* may not actually exist on the    */
  91.                                 /* network, sending this packet     */
  92.                                 /* to OT should at least generate   */
  93.                                 /* AARP request packets to resolve  */
  94.                                 /* the destNodeID to ELAP address.  */
  95.  
  96.                                 /* fill out the destination         */
  97.                                 /* DDPAddress struct based on the   */
  98.                                 /* values stuffed in the DDP Packet    */
  99.  
  100.     sendAddr.fAddressType     = AF_ATALK_DDP;    
  101.     sendAddr.fNetwork        = retDDPAddr.fNetwork;
  102.     sendAddr.fNodeID        = kDestNodeID;
  103.     sendAddr.fSocket        = 4;    // echo socket
  104.     sendAddr.fDDPType        = 4;    // echo packet type
  105.  
  106.     opt = (TOption*)buf;        // set option ptr to buffer
  107.  
  108.     opt->level    = ATK_DDP;                    // dealing with tpi
  109.     opt->name    = DDP_OPT_SRCADDR;
  110.     opt->len    = kOTOptionHeaderSize + kDDPAddressLength;
  111.     opt->status = 0;
  112.         // move the mnode src address into the value field
  113.     BlockMove((Ptr)&retDDPAddr, (Ptr)&opt->value, kDDPAddressLength); 
  114.  
  115.                                 /* fill out the OpenTransport Param */
  116.                                 /* block for sending data            */
  117.     otPB.addr.maxlen        = 0;
  118.     otPB.addr.len            = sizeof(DDPAddress);
  119.     otPB.addr.buf            = (UInt8*)&sendAddr;
  120.     otPB.opt.maxlen            = 0;
  121.     otPB.opt.len            = kOTOptionHeaderSize + kDDPAddressLength;
  122.     otPB.opt.buf            = (UInt8*)opt;
  123.     otPB.udata.maxlen        = 0;
  124.     otPB.udata.len            = 20;    // send only the first 20 bytes of the data buffer
  125.     otPB.udata.buf            = (UInt8 *)&packet;
  126.     
  127.     packet[0] = 1;            // set the echo packet as a request
  128.     
  129.                                 /* send the data over the endpoint. */
  130.                                 /* As noted above, at a minimum,     */
  131.                                 /* AARP packets should be sent to    */
  132.                                 /* resolve the DDP nodeID.            */
  133.  
  134.     otErr = OTSndUData(ep, &otPB);
  135.     if (otErr != kOTNoError) 
  136.     {
  137.     
  138.         DebugStr("\p Failed to send data");
  139.     }
  140.  
  141.         // set up a watchdog timer for 3 seconds
  142.     timer = TickCount() + 3 * 60;
  143.         // assume an incoming packet will be received
  144.     processIncoming = true;
  145.                             
  146.     while((OTLook(ep) != T_DATA) && processIncoming)
  147.     {
  148.         if (timer < TickCount())
  149.         {
  150.             printf("\n\n No Echo reply received");
  151.             processIncoming = false;    // no packet recieved
  152.         }
  153.     }
  154.  
  155.     if (processIncoming)
  156.     {
  157.         printf("\n\n incoming echo response to multinode");
  158.         otPB.addr.maxlen         = sizeof(recvAddr);
  159.         otPB.addr.len            = 0;
  160.         otPB.addr.buf            = (UInt8*)&recvAddr;
  161.         otPB.opt.maxlen            = 0;
  162.         otPB.opt.len            = 0;
  163.         otPB.opt.buf            = 0;
  164.         otPB.udata.maxlen        = sizeof(packet);
  165.         otPB.udata.len            = 0;
  166.         otPB.udata.buf            = (UInt8*)packet;
  167.                 
  168.         otErr = OTRcvUData(ep, &otPB, &flags);
  169.         if (otErr == kOTNoError)
  170.             printf("\n\n incoming packet processed OK");
  171.         else
  172.             printf("\n\n error occurred processing incoming packet");
  173.  
  174.     }
  175.  
  176.     OTCloseProvider(ep);
  177.     
  178.     return(0);
  179. }
  180.  
  181.  
  182.